// @APIVersion 1.0.0
// @Title beego Test API
// @Description beego has a very cool tools to autogenerate documents for your API
// @Contact astaxie@gmail.com
// @TermsOfServiceUrl http://beego.me/
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers

import (
	"chess_platform/controllers/client"
	"chess_platform/controllers/h5"
	"chess_platform/controllers/pb"
	"chess_platform/controllers/pc"
	"chess_platform/controllers/pf"
	"chess_platform/internal/common"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/plugins/cors"
)

func init() {
	beego.InsertFilter("/*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowOrigins:     []string{"http://*"},
		AllowMethods:     []string{"PUT", "POST", "DELETE"},
		AllowHeaders:     []string{"Origin", "Accept", "Content-Type", "Authorization", "token"},
		ExposeHeaders:    []string{"Content-length"},
		AllowCredentials: true,
	}))
	beego.InsertFilter("/v1/client/*", beego.BeforeRouter, OnlyAllowClientAccess)
	beego.InsertFilter("/v1/pc/*", beego.BeforeRouter, OnlyAllowPctAccess)
	beego.InsertFilter("/v1/h5/*", beego.BeforeRouter, OnlyAllowH5tAccess)

	/*
		beego.InsertFilter("/v1/client/weChat-login", beego.BeforeRouter, FilterAllAccessLimit)
		beego.InsertFilter("/v1/h5/weChat-login", beego.BeforeRouter, FilterAllAccessLimit)
		beego.InsertFilter("/v1/h5/login", beego.BeforeRouter, FilterAllAccessLimit)
		beego.InsertFilter("/v1/pc/login", beego.BeforeRouter, FilterAllAccessLimit)
	*/

	ns := beego.NewNamespace("/v1",
		//--------------------以下为游戏客户端路由---------------------------
		beego.NSNamespace("/client/user",
			beego.NSInclude(
				&client.UserController{},
			),
		),
		beego.NSNamespace("/client",
			beego.NSInclude(
				&client.LoginController{},
				&client.GameController{},
				&client.OtherController{},
				&client.NoticeController{},
				&client.LogController{},
			),
		),
		beego.NSNamespace("/client/public",
			beego.NSInclude(
				&client.PublicController{},
			),
		),
		beego.NSNamespace("/client/email",
			beego.NSInclude(
				&client.EmailController{},
			),
		),
		beego.NSNamespace("/client/club",
			beego.NSInclude(
				&client.ClubController{},
			),
		),
		beego.NSNamespace("/client/game",
			beego.NSInclude(
				&client.GameInfoController{},
			),
		),
		beego.NSNamespace("/client/pay",
			beego.NSInclude(
				&client.PayController{},
			),
		),
		beego.NSNamespace("/client/conf",
			beego.NSInclude(
				&client.ConfController{},
			),
		),
		beego.NSNamespace("/client/active",
			beego.NSInclude(
				&client.ActiveController{},
			),
		),
		beego.NSNamespace("/client/contest",
			beego.NSInclude(
				&client.ContestController{},
			),
		),
		beego.NSNamespace("/client/lottery",
			beego.NSInclude(
				&client.LotteryController{},
			),
		),
		//--------------------以下为代理助手路由---------------------------
		beego.NSNamespace("/h5/user",
			beego.NSInclude(
				&h5.UserController{},
				&h5.AgentController{},
			),
		),
		beego.NSNamespace("/h5",
			beego.NSInclude(
				&h5.LoginController{},
				&h5.PayController{},
				&h5.OssController{},
			),
		),
		beego.NSNamespace("/h5/public",
			beego.NSInclude(
				&h5.PublicController{},
			),
		),
		beego.NSNamespace("/h5/card",
			beego.NSInclude(
				&h5.CardController{},
				&h5.OrderController{},
			),
		),
		beego.NSNamespace("/h5/cash",
			beego.NSInclude(
				&h5.CashController{},
			),
		),
		beego.NSNamespace("/h5/conf",
			beego.NSInclude(
				&h5.ConfController{},
			),
		),
		beego.NSNamespace("/h5/client",
			beego.NSInclude(
				&h5.ClientController{},
			),
		),

		//--------------------以下为pc路由---------------------------
		//游戏设置
		beego.NSNamespace("/pc/game",
			beego.NSInclude(
				&pc.GameController{},
			),
		),
		//数据统计
		beego.NSNamespace("/pc/stat",
			beego.NSInclude(
				&pc.StatController{},
			),
		),
		//运营管理
		beego.NSNamespace("/pc/run",
			beego.NSInclude(
				&pc.RunController{},
			),
		),
		//运维管理
		beego.NSNamespace("/pc/operation",
			beego.NSInclude(
				&pc.OperationController{},
			),
		),
		//数据监控
		beego.NSNamespace("/pc/monitor",
			beego.NSInclude(
				&pc.MonitorController{},
			),
		),
		//玩家管理
		beego.NSNamespace("/pc/player",
			beego.NSInclude(
				&pc.PlayerController{},
			),
		),
		//代理管理
		beego.NSNamespace("/pc/agent",
			beego.NSInclude(
				&pc.AgentController{},
			),
		),
		//财务管理
		beego.NSNamespace("/pc/finance",
			beego.NSInclude(
				&pc.FinanceController{},
			),
		),
		//帐户管理
		beego.NSNamespace("/pc/user",
			beego.NSInclude(
				&pc.UserController{},
			),
		),
		//日志管理
		beego.NSNamespace("/pc/log",
			beego.NSInclude(
				&pc.LogController{},
			),
		),
		//公共模块: 不需要权限认证的,但仍需要token
		beego.NSNamespace("/pc/public",
			beego.NSInclude(
				&pc.PublicController{},
			),
		),
		//登录,不需要token
		beego.NSNamespace("/pc",
			beego.NSInclude(
				&pc.LoginController{},
			),
		),
		//配置中心
		beego.NSNamespace("/pc/conf",
			beego.NSInclude(
				&pc.ConfController{},
			),
		),
		//配置中心
		beego.NSNamespace("/pc/pb",
			beego.NSInclude(
				&pc.PartnerController{},
			),
		),
		//抽奖系统
		beego.NSNamespace("/pc/lottery",
			beego.NSInclude(
				&pc.LotteryController{},
			),
		),
		//测试接口,不要对接
		beego.NSNamespace("/pc/test",
			beego.NSInclude(
				&pc.TestController{},
			),
		),
		//---------------------以下是partner端的路由----------------------------
		beego.NSNamespace("/pf",
			beego.NSInclude(
				&pf.LoginController{},
				&pf.PayController{},
				&pf.OssController{},
			),
		),

		beego.NSNamespace("/pf/user",
			beego.NSInclude(
				&pf.UserController{},
				&pf.AgentController{},
			),
		),
		beego.NSNamespace("/pf/card",
			beego.NSInclude(
				&pf.CardController{},
				&pf.OrderController{},
			),
		),
		beego.NSNamespace("/pf/cash",
			beego.NSInclude(
				&pf.CashController{},
			),
		),
		beego.NSNamespace("/pf/conf",
			beego.NSInclude(
				&pf.ConfController{},
			),
		),
		beego.NSNamespace("/pf/place",
			beego.NSInclude(
				&pf.PlaceController{},
				&pf.PartnerController{},
			),
		),

		//--------------------以下为合伙人后台系统路由---------------------------
		//帐户管理
		beego.NSNamespace("/pb/user",
			beego.NSInclude(
				&pb.UserController{},
			),
		),
		//登录,不需要token
		beego.NSNamespace("/pb",
			beego.NSInclude(
				&pb.LoginController{},
			),
		),
		//代理管理
		beego.NSNamespace("/pb/agent",
			beego.NSInclude(
				&pb.AgentController{},
			),
		),
		//财务管理
		beego.NSNamespace("/pb/finance",
			beego.NSInclude(
				&pb.FinanceController{},
			),
		),
		//首页
		beego.NSNamespace("/pb/home",
			beego.NSInclude(
				&pb.HomeController{},
			),
		),
	)

	beego.AddNamespace(ns)
}

var FilterAllAccessLimit = func(ctx *context.Context) {
	//TODO:暂时写死, 10秒钟之内最多只能访问一万次
	filterAllAccessLimit(ctx, 10000, 10)
}

var filterAllAccessLimit = func(ctx *context.Context, frequency, expireTime int64) {
	if !common.AccessLimit(ctx.Input.IP(), frequency, expireTime) {
		response := map[string]interface{}{
			"code": 100,
			"msg":  "操作过于频繁，请稍候再操作",
		}
		ctx.Output.JSON(response, true, true)
		return
	}
}

var OnlyAllowPctAccess = func(ctx *context.Context) {
	//common.URLCount[ctx.Input.URL()]++
	common.Load++
	if common.PlatformConf.Type != common.PlatformPc && common.PlatformConf.Type != 0 {
		response := map[string]interface{}{
			"code": 410,
			"msg":  "拒绝访问",
		}
		ctx.Output.JSON(response, true, true)
		return
	}
}
var OnlyAllowH5tAccess = func(ctx *context.Context) {
	//common.URLCount[ctx.Input.URL()]++
	common.Load++
	if common.PlatformConf.Type != common.PlatformH5 && common.PlatformConf.Type != 0 {
		response := map[string]interface{}{
			"code": 410,
			"msg":  "拒绝访问",
		}
		ctx.Output.JSON(response, true, true)
		return
	}
}
var OnlyAllowClientAccess = func(ctx *context.Context) {
	//common.URLCount[ctx.Input.URL()]++
	common.Load++
	if common.PlatformConf.Type != common.PlatformClient && common.PlatformConf.Type != 0 {
		response := map[string]interface{}{
			"code": 410,
			"msg":  "拒绝访问",
		}
		ctx.Output.JSON(response, true, true)
		return
	}
}
